package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.GrouponReqBo;
import com.abaaba.project2.bean.bo.GrouponRuleCreateBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.mapper.GrouponMapper;
import com.abaaba.project2.mapper.GrouponRulesMapper;
import com.abaaba.project2.service.AdminGrouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.Date;
import java.util.List;

@Service
public class AdminGrouponServiceImpl implements AdminGrouponService {
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BaseData queryGrouponRuleList(GrouponReqBo grouponReqBo) {
        PageHelper.startPage(grouponReqBo.getPage(), grouponReqBo.getLimit());

        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (grouponReqBo.getGoodsId() != null && !"".equals(String.valueOf(grouponReqBo.getGoodsId()))) {
            criteria.andGoodsIdEqualTo(grouponReqBo.getGoodsId());
        }
        grouponRulesExample.setOrderByClause(grouponReqBo.getSort() + " " + grouponReqBo.getOrder());
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        return BaseData.ok(grouponRules, grouponRulesPageInfo.getTotal());
    }

    @Override
    public GrouponRules insertGrouponRule(GrouponRuleCreateBo createBo) {
        Goods goods = goodsMapper.selectByPrimaryKey(createBo.getGoodsId());
        GrouponRules grouponRules = new GrouponRules();
        grouponRules.setGoodsId(createBo.getGoodsId());
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setDiscount(createBo.getDiscount());
        grouponRules.setDiscountMember(createBo.getDiscountMember());
        grouponRules.setAddTime(new Date(java.lang.System.currentTimeMillis()));
        grouponRules.setUpdateTime(grouponRules.getAddTime());
        grouponRules.setExpireTime(createBo.getExpireTime());
        grouponRules.setDeleted(false);
        grouponRulesMapper.insertSelective(grouponRules);
        return grouponRules;
    }

    @Override
    public Integer updateGrouponRule(GrouponRules grouponRules) {
        return grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
    }

    @Override
    public Integer deleteGrouponRule(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        return grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
    }

    @Override

    public BaseData<Groupon> queryGrouponList(GrouponReqBo grouponReqBo) {
//        PageHelper.startPage(grouponReqBo.getPage(), grouponReqBo.getLimit());
//
//        GrouponExample grouponExample = new GrouponExample();
//        GrouponExample.Criteria criteria = grouponExample.createCriteria();
//        criteria.andDeletedEqualTo(false);
//        if (grouponReqBo.getGoodsId() != null && !"".equals(String.valueOf(grouponReqBo.getGoodsId()))) {
////            grouponRulesMapper
//            criteria.andRulesIdEqualTo(grouponReqBo.getGoodsId());
//        }
//        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
//        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);

//        return BaseData.ok(groupons, grouponPageInfo.getTotal());
        return null;
    }


}
